// pagesLuxian/pages/guiHua/index.js
const app = getApp();
import {
  getAddress,
  getKeyAddress,
  getlatlngAddress,
  getLnglat
} from "../../../utils/address";
import {
  getStorage,
  setStorage
} from "../../../utils/localStorage";
import {
  generateRoute,
  peopleGenerate_Stat
} from "../../../api/luxian";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //自定义头部组件参数
    navData: {
      title: '智能规划',
      level: 10,
    },
    //自定义导航栏高度
    pageTop: app.globalData.navHeight + app.globalData.navTop + 10,
    pageBottom: app.globalData.bottomLift,
    //用户信息
    userInfo: {},
    //出行类型
    travelTypeData: ['自驾游'],
    //游玩类型列表
    playTypeData: [],
    //时间数据
    dateTime: [],
    //选择时间索引
    xzDateIndex: 0,
    //表单数据
    form: {},
    //页面数据
    infoData: {},
    //地址列表
    addressList: [],
    //显示弹窗状态
    boxIndex: 1,
    //makers标记点数据
    markerData: [
      //   {
      //     id: 1,
      //     longitude: '119.62',
      //     latitude: '29.12',
      //     joinCluster:false. //是否参与聚合点
      //     zIndex:0,   //显示层级
      //     iconPath:'',//显示的图标
      //     width:'',    //图标的宽度
      //     height:'',   //图表的高度
      //     label: {      //为标记点旁边增加标签
      //         content: `当前位置`,
      //         color: '#4646A0',
      //         fontSize: 14,
      //         anchorX: 0,
      //         anchorY: 0,
      //     },
      //   }
    ],
    //选择路线类型
    lxianType: undefined,
    //地图组件参数
    mapData: {
      scale: 16, //缩放级别，取值范围为3-20
      minScale: 3, //最小缩放级别
      maxScale: 20, //最大缩放级别
      enable3D: true, //展示3D楼块
      enableZoom: true, //是否支持缩放
      enableScroll: true, //是否支持拖动
      enableBuilding: true, //是否展示建筑物
      tapFlag: false, //是否支持点击地图打开第三方地图功能
    },
    latLng: null,
    weekArr: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    startTimestamp: 0,
    endTimestamp: 0,
    //选择时间段的点击状态
    clickState: false,
    mapCtx: null,
    //倒计时
    djsIndex: 0,
    //map地图中心位置经纬度
    longitude: null,
    latitude: null,
    //map组件地图bindmaker和bindtap，节流状态
    clickFlag: true,
    //搜索地址列表
    searchList: [],
    searchFlag: false,
    //推荐目的地列表
    reDestionList: [],
    //搜索内容
    searchValue: null,
    //选择地址数据        
    sectAddressData: {
      value: undefined,
      longitude: undefined,
      latitude: undefined
    },
    //开始距离 
    startDistance: 80,
    //结束距离
    endDistance: 90,
    //当天时间
    toDay: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.reset();
    this.setData({
      userInfo: getStorage('user'),
    });
    this.getCurrentAddress();
    this.getYouwanTypeList();
    this.daTravelDays();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  //初始化form表单数据
  reset() {
    this.setData({
      form: {
        userId: this.data.userInfo.id,
        startPosition: undefined,
        startLongitude: undefined,
        startLatitude: undefined,
        endPosition: undefined,
        endLongitude: undefined,
        endLatitude: undefined,
        startTime: undefined,
        startDate: undefined,
        endTime: undefined,
        endDate: undefined,
        villageLabel: '0',
        villageValue: '休闲度假',
        travelMode: '自驾游',
        dayNumber: 0,
        stayNum: 0,
      }
    });
  },
  //页面左上角返回按钮操作事件
  back() {
    const _this = this.data;
    if (this.data.boxIndex === 2) {
      this.setData({
        sectAddressData: {
          value: undefined,
          longitude: undefined,
          latitude: undefined,
        },
        searchFlag: false,
        boxIndex: 1,
      });
      console.log(_this.markerData, 'markerData');
      console.log(_this.form, 'form');
      if (!_this.form.endPosition) {
        this.getCurrentAddress();
      } else {
        // const arr = _this.markerData;
      };

    } else {
      wx.navigateBack();
    };
  },
  //获取当前地址
  getCurrentAddress() {
    wx.showLoading({
      title: "地图加载中,请稍后...",
      mask: true
    });
    getAddress().then(res => {
      const {
        address,
        location
      } = res.result;
      this.setData({
        longitude: location.lng,
        latitude: location.lat,
        "form.startPosition": address,
        "form.startLongitude": location.lng,
        "form.startLatitude": location.lat,
        latLng: location.lng + "," + location.lat
      });
      this.getMapVillage();
    });
  },

  /** 获取地图上的村庄数据 */
  getMapVillage() {
    const _this = this.data;
    peopleGenerate_Stat({
      longitude: _this.longitude,
      latitude: _this.latitude,
      startDistance: _this.startDistance,
      endDistance: _this.endDistance
    }).then(res => {
      let arr = [];
      res.data.data.villages.forEach((item, index) => {
        arr.push({
          id: item.villageId,
          iconPath: "../../../image/icon/xcAddress_x.png",
          width: 14,
          height: 14,
          label: {
            content: item.villageName,
            color: '#ffffff',
            fontSize: 12,
            anchorX: -20,
            anchorY: 0,
          },
          longitude: item.longitude,
          latitude: item.latitude,
          joinCluster: false,
        });
      });
      arr.unshift({
        id: 99999,
        iconPath: "../../../image/icon/dqAddress_x.png",
        width: 30,
        height: 30,
        label: {
          content: '当前位置',
          color: '#ffffff',
          fontSize: 12,
          anchorX: -20,
          anchorY: 0,
        },
        longitude: _this.longitude,
        latitude: _this.latitude,
        joinCluster: false,
      });
      this.setData({
        infoData: res.data.data,
        markerData: arr,
      });
      wx.hideLoading();
      // this.initAggregatePoints();
    });
  },

  /** 初始化聚合点 */
  // initAggregatePoints() {
  //     this.mapCtx = wx.createMapContext('map');
  //     this.mapCtx.initMarkerCluster({
  //         enableDefaultStyle: false,
  //         zoomOnClick: true,
  //         gridSize: 100,
  //         complete(res) {
  //             console.log(res, '初始化聚合点');
  //         }
  //     });
  //     this.mapCtx.on('markerClusterCreate', res => {
  //         const clusters = res.clusters
  //         const markers = clusters.map(cluster => {
  //             const {
  //                 center,
  //                 clusterId,
  //                 markerIds
  //             } = cluster
  //             return {
  //                 ...center,
  //                 width: 0,
  //                 height: 0,
  //                 clusterId,
  //                 label: {
  //                     content: '您附近有' + markerIds.length + '个乡村',
  //                     fontSize: 10,
  //                     color: '#fff',
  //                     width: 60,
  //                     height: 60,
  //                     borderWidth: 0,
  //                     borderColor: "#4846A7",
  //                     bgColor: "#4846A7",
  //                     borderRadius: 100,
  //                     textAlign: 'center',
  //                     anchorX: 0,
  //                     anchorY: -60,
  //                 }
  //             }
  //         })
  //         this.mapCtx.addMarkers({
  //             markers,
  //             clear: false,
  //         });
  //     });
  // },

  /** 出行时间处理 */
  daTravelDays() {
    let date = new Date(),
      cYear = date.getFullYear(),
      cMonth = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1),
      cDay = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    this.setData({
      toDay: cYear + '-' + cMonth + '-' + cDay,
    });
    this.dateFunc(cYear + '-' + cMonth + '-' + cDay, 7);
  },

  /*  根据选择的时间计算后 n 天的时间 */
  dateFunc(date, day) {
    // 将选择的日期转换为毫秒数
    const selectedTimestamp = new Date(date.replace(/-/g, '/')).getTime();

    // 计算后 n 天的时间的毫秒数
    const nextDayTimestamp = selectedTimestamp + (day * 24 * 60 * 60 * 1000);

    // 将毫秒数转换为 Date 对象
    const nextDayDate = new Date(nextDayTimestamp);

    // 计算相差的毫秒数
    const timeDiff = Math.abs(new Date(nextDayDate).getTime() - new Date(date).getTime());

    // 将相差的毫秒数转换为天数
    const daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));

    let arr = [],
      currentTimestamp = null,
      futureTimestamp = null,
      rdDate = null,
      cYear = null,
      cMonth = null,
      cDay = null,
      cWeek = null;

    for (let i = 0; i < daysDiff; i++) {

      // 获取当前日期的时间戳
      const currentTimestamp = new Date(date).getTime();

      // 计算后 n 天的时间戳
      futureTimestamp = currentTimestamp + (i * 24 * 60 * 60 * 1000);

      rdDate = new Date(futureTimestamp);
      cYear = rdDate.getFullYear(),
        cMonth = (rdDate.getMonth() + 1) < 10 ? '0' + (rdDate.getMonth() + 1) : (rdDate.getMonth() + 1),
        cDay = rdDate.getDate() < 10 ? '0' + rdDate.getDate() : rdDate.getDate(),
        cWeek = rdDate.getDay();
      arr.push({
        year: cYear,
        month: cMonth,
        day: cDay,
        week: cWeek,
        Time: cYear + '-' + cMonth + '-' + cDay,
        timeStamp: new Date(cYear + '/' + cMonth + '/' + cDay).getTime()
      });
    };
    const timeDifference = arr[0].timeStamp - arr[1].timeStamp,
      dayDifference = Math.ceil(timeDifference / (1000 * 3600 * 24));
    this.setData({
      dateTime: arr,
      "form.startTime": arr[0].Time,
      "form.startDate": arr[0].month + '-' + arr[0].day + this.data.weekArr[arr[0].week],
      startTimestamp: arr[0].timeStamp,
      "form.endTime": arr[1].Time,
      "form.endDate": arr[1].month + '-' + arr[1].day + this.data.weekArr[arr[1].week],
      endTimestamp: arr[1].timeStamp,
      "form.dayNumber": Math.abs(dayDifference) === 0 ? 1 : Math.abs(dayDifference),
      "form.stayNum": Math.abs(dayDifference) + 1,
    });
  },

  //更换按钮点击事件
  genhuanTap(e) {
    const type = e.currentTarget.dataset.type,
      _this = this.data;
    this.setData({
      boxIndex: 2,
      lxianType: type
    });
    const longitude = this.data.lxianType === "start" ? this.data.form.startLongitude : this.data.lxianType === "end" && this.data.form.endLongitude ? this.data.form.endLongitude : this.data.form.startLongitude,
      latitude = this.data.lxianType === "start" ? this.data.form.startLatitude : (this.data.lxianType === "end" && this.data.form.endLatitude) ? this.data.form.endLatitude : this.data.form.startLatitude;

    this.obtionDestin(longitude, latitude);
  },

  //地址搜索栏input框bindinput事件
  searchAddress(e) {
    const value = e.detail.value;
    if (value) {
      getKeyAddress(value).then(res => {
        this.setData({
          searchList: res,
          searchValue: value,
          searchFlag: true
        });
      });
    } else {
      this.setData({
        searchFlag: false
      });
    };
  },

  //搜索input框 blur失去焦点事件
  searchAddressBlur() {
    this.setData({
      searchFlag: false
    });
  },

  //搜索按钮点击事件
  searchButtonTap() {
    const _this = this.data;
    getLnglat(this.data.searchValue).then(res => {
      const {
        address_components,
        location,
        title
      } = res;
      this.setData({
        sectAddressData: {
          value: address_components.province + address_components.city + address_components.district + title,
          longitude: location.lng,
          latitude: location.lat,
        },
        searchFlag: false,
      });
      this.obtionDestin(_this.sectAddressData.longitude, _this.sectAddressData.latitude);
    });
  },

  //搜索地址列表点击事件
  searchListTap(e) {
    const item = e.currentTarget.dataset.item,
      _this = this.data;
    this.setData({
      searchValue: item.title,
      sectAddressData: {
        value: item.address,
        longitude: item.location.lng,
        latitude: item.location.lat,
      },
      searchFlag: false,
    });
    this.obtionDestin(item.location.lng, item.location.lat);
  },

  //获取推荐目的地
  obtionDestin(longitude, latitude) {
    const _this = this.data;
    const arr = [];
    peopleGenerate_Stat({
      longitude: _this.longitude,
      latitude: _this.latitude,
      startDistance: _this.startDistance,
      endDistance: _this.endDistance
    }).then(res => {
      res.data.data.villages.forEach((item, index) => {
        item.villageLabel = item.villageLabel ? item.villageLabel.split(",") : [];
        arr.push({
          id: item.villageId,
          iconPath: (item.longitude !== longitude) && (item.latitude !== latitude) ? "../../../image/icon/xcAddress_x.png" : "../../../image/icon/dqAddress_x.png",
          width: (item.longitude !== longitude) && (item.latitude !== latitude) ? 14 : 30,
          height: (item.longitude !== longitude) && (item.latitude !== latitude) ? 14 : 30,
          label: {
            content: item.villageName,
            color: '#ffffff',
            fontSize: 12,
            anchorX: -20,
            anchorY: 0,
          },
          longitude: item.longitude,
          latitude: item.latitude,
          joinCluster: true,
        });
      });
      //判断数组里是否已经存在当前位置
      let arrFlag = false;
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].longitude === longitude && arr[i].latitude === latitude) {
          arrFlag = false;
          break;
        } else {
          arrFlag = true;
        };
      };
      if (arrFlag) {
        arr.unshift({
          id: 99999,
          iconPath: "../../../image/icon/dqAddress_x.png",
          width: 30,
          height: 30,
          label: {
            content: "当前位置",
            color: '#ffffff',
            fontSize: 12,
            anchorX: -20,
            anchorY: 0,
          },
          longitude: longitude,
          latitude: latitude,
          joinCluster: true,
        });
      };

      this.setData({
        reDestionList: res.data.data.villages,
        longitude: longitude,
        latitude: latitude,
        markerData: arr,
        searchFlag: false,
      });
    });
  },

  //推荐目的地列表点击事件
  redeListTap(e) {
    let item = e.currentTarget.dataset.item,
      index = e.currentTarget.dataset.index;
    let arr = this.data.markerData.map(val => {
      if (val.id === item.villageId) {
        val.iconPath = '../../../image/icon/xzAddress_x.png';
        val.width = 30;
        val.height = 30;
        val.label.content = val.label.content.indexOf("查看详情") === -1 ? `${val.label.content}\n查看详情` : val.label.content;
      } else if (val.id === 99999) {
        val.iconPath = '../../../image/icon/dqAddress_x.png';
        val.width = 30;
        val.height = 30;

      } else {
        val.iconPath = '../../../image/icon/xcAddress_x.png';
        val.width = 14;
        val.height = 14;
        val.label.content = val.label.content.indexOf("查看详情") === -1 ? val.label.content : val.label.content.slice(0, val.label.content.length - 5);
      };
      return val;
    });

    this.setData({
      longitude: item.longitude,
      latitude: item.latitude,
      markerData: arr,
      xzAddressValue: item.villageAddress,
      sectAddressData: {
        value: item.villageAddress,
        longitude: item.longitude,
        latitude: item.latitude,
      },
    });
  },

  //确定按钮点击事件
  quxiaoTap() {
    const _this = this.data,
      emptFlag = Object.values(_this.sectAddressData).some(item => !item);
    if (emptFlag) {
      wx.showToast({
        icon: 'none',
        mask: true,
        title: `请选择${this.data.lxianType === 'start' ? '出发地' : '目的地'}`,
        duration: 2000,
      });
      return;
    };
    if (_this.lxianType === "start") {
      this.setData({
        "form.startPosition": _this.sectAddressData.value,
        "form.startLongitude": _this.sectAddressData.longitude,
        "form.startLatitude": _this.sectAddressData.latitude,
      });
    } else {
      this.setData({
        "form.endPosition": _this.sectAddressData.value,
        "form.endLongitude": _this.sectAddressData.longitude,
        "form.endLatitude": _this.sectAddressData.latitude,
      });
    };
    this.setData({
      boxIndex: 1,
    });
  },
  //获取游玩类型列表
  getYouwanTypeList() {
    app.getDict("village_label").then(res => {
      const arr = res.data.filter(item => item.dictLabel !== "全部");
      this.setData({
        playTypeData: arr
      });
    });
  },

  //自驾类型picker change事件
  travelPlayChange(e) {
    const index = +e.detail.value;
    this.setData({
      "form.travelMode": this.data.travelTypeData[index]
    })
  },

  //游玩类型picker change事件
  playPlayChange(e) {
    const index = +e.detail.value;
    this.setData({
      "form.villageLabel": this.data.playTypeData[index].dictValue,
      "form.villageValue": this.data.playTypeData[index].dictLabel
    });
  },

  //计划出行时间picker change事件
  startDateChange(e) {
    const value = e.detail.value,
      date = new Date(value.replace(/-/g, '/')),
      week = date.getDay(),
      month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1),
      day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    this.setData({
      "form.startTime": value,
      "form.startDate": month + '-' + day + this.data.weekArr[week],
      startTimestamp: date.getTime(),
    });
    console.log(value);
    this.dateFunc(value, 7);
  },

  //计划结束时间picker change事件
  endDateChange(e) {
    const value = e.detail.value,
      endTime = Date.parse(value),
      startTime = Date.parse(this.data.form.startTime),
      timeDifference = startTime - endTime,
      dayDifference = Math.ceil(timeDifference / (1000 * 3600 * 24)),
      date = new Date(value.replace(/-/g, '/')),
      week = date.getDay(),
      month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1),
      day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    if (endTime >= startTime) {
      this.setData({
        "form.endTime": e.detail.value,
        "form.endDate": month + '-' + day + this.data.weekArr[week],
        "form.dayNumber": Math.abs(dayDifference) === 0 ? 1 : Math.abs(dayDifference),
        "form.stayNum": Math.abs(dayDifference) + 1,
        endTimestamp: date.getTime(),
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '计划结束时间不能小于计划出行时间',
        duration: 3000
      });
    };
  },

  //选择时间点击事件
  dateListTimeTap(e) {
    let index = e.currentTarget.dataset.index,
      state = e.currentTarget.dataset.state,
      date = new Date(this.data.dateTime[index].Time.replace(/-/g, '/'));
    if (!state) {
      this.setData({
        "form.startTime": null,
        "form.startDate": null,
        startTimestamp: null,
        "form.endTime": null,
        "form.endDate": null,
        endTimestamp: null,
        "form.dayNumber": 0,
        "form.stayNum": 0,
        "form.startTime": this.data.dateTime[index].Time,
        "form.startDate": this.data.dateTime[index].month + '-' + this.data.dateTime[index].day + this.data.weekArr[this.data.dateTime[index].week],
        startTimestamp: date.getTime(),
      });
    } else {
      if (date.getTime() >= this.data.startTimestamp) {
        const endTime = this.data.dateTime[index].timeStamp,
          timeDifference = this.data.startTimestamp - endTime,
          dayDifference = Math.ceil(timeDifference / (1000 * 3600 * 24));
        this.setData({
          "form.endTime": this.data.dateTime[index].Time,
          "form.endDate": this.data.dateTime[index].month + '-' + this.data.dateTime[index].day + this.data.weekArr[this.data.dateTime[index].week],
          "form.dayNumber": Math.abs(dayDifference) === 0 ? 1 : Math.abs(dayDifference),
          "form.stayNum": Math.abs(dayDifference) + 1,
          endTimestamp: date.getTime(),
        });
      };
    };
    this.setData({
      clickState: !state
    });
  },

  //开始旅行按钮点击事件
  generateTap() {
    if (!this.data.form.endPosition) {
      wx.showToast({
        icon: 'none',
        title: '请选择目的地',
        duration: 2000
      });
    } else if (!this.data.form.startTime) {
      wx.showToast({
        icon: 'none',
        title: '请选择计划出行时间',
        duration: 2000
      });
    } else if (!this.data.form.endTime) {
      wx.showToast({
        icon: 'none',
        title: '请选择计划结束时间',
        duration: 2000
      });
    } else {
      setStorage("pageId", "travel");
      wx.navigateTo({
        url: '/pagesLuxian/pages/loading/index?form=' + encodeURI(JSON.stringify(this.data.form)),
      });
    };
  },


  //地图点击事件
  destinationMapTap(e) {
    //利用节流处理bindmarkertap会触发bindtap事件的问题
    if (!this.data.clickFlag) return;
    this.setData({
      clickFlag: false
    });

    //执行的代码块
    if (e.detail.markerId !== 99999) {
      this.inplMapDataFunc(e);
    };

    setTimeout(() => {
      this.setData({
        clickFlag: true
      });
    }, 500);
  },

  //地图层级放大缩小change事件
  bindreginonchange(e) {
    if (e.causedBy !== 'scale') return;
    const scale = parseInt(e.detail.scale);
    let startDistance = 0,
      endDistance = 0;
    console.log(scale, 'scale');
    switch (scale) {
      case 8:
        startDistance = 80;
        endDistance = 90;
        break;
      case 9:
        startDistance = 40;
        endDistance = 50;
        break;
      case 10:
        startDistance = 20;
        endDistance = 30;
        break;
      case 11:
        startDistance = 0;
        endDistance = 20;
        break;
      case 12:
        startDistance = 0;
        endDistance = 20;
        break;
    };
    this.setData({
      startDistance: startDistance,
      endDistance: endDistance,
      markerData: []
    });
    this.getMapVillage();
  },

  //智能规划地图点击时处理数据的函数
  inplMapDataFunc(e) {
    const _this = this.data;
    let makerArr = this.data.markerData.filter(item => item.id !== 9999999);
    if (e.type === "markertap" || e.type === "labeltap") {
      let index = this.data.markerData.map(item => item.id).indexOf(e.detail.markerId),
        obj = this.data.markerData[index];
      if (obj.label.content.indexOf("查看详情") === -1) {
        let arr = makerArr.map(item => {
          if (e.detail.markerId === item.id) {
            item.width = 30;
            item.height = 30;
            item.iconPath = item.label.content.indexOf("查看详情") === -1 ? '../../../image/icon/xzAddress_x.png' : item.iconPath;
            item.label.content = item.label.content.indexOf("查看详情") === -1 ? `${item.label.content}\n查看详情` : item.label.content;
            item.label.fontSize = 14;
            item.label.anchorX = -25;
          } else {
            item.iconPath = item.id !== 99999 ? '../../../image/icon/xcAddress_x.png' : '../../../image/icon/dqAddress_x.png';
            item.width = item.id !== 99999 ? 14 : 30;
            item.height = item.id !== 99999 ? 14 : 30;
            item.label.content = item.label.content.indexOf("查看详情") === -1 ? item.label.content : item.label.content.slice(0, item.label.content.length - 5);
            item.label.fontSize = 12;
            item.label.anchorX = -25;
          };
          return item;
        });
        this.setData({
          longitude: obj.longitude,
          latitude: obj.latitude,
          "form.endPosition": obj.label.content.indexOf("查看详情") === -1 ? obj.label.content : obj.label.content.slice(0, obj.label.content.length - 5),
          "form.endLongitude": obj.longitude,
          "form.endLatitude": obj.latitude,
          markerData: arr,
        });
        if (_this.boxIndex === 2) {
          this.setData({
            sectAddressData: {
              value: obj.label.content.indexOf("查看详情") === -1 ? obj.label.content : obj.label.content.slice(0, obj.label.content.length - 5),
              longitude: obj.longitude,
              latitude: obj.latitude,
            }
          });
          return;
        };
      } else {
        wx.navigateTo({
          url: '/pagesShouYe/pages/xaingcunDetail/index?id=' + obj.id,
        });
      };
    } else {
      makerArr.forEach(item => {
        item.label.content = item.label.content.indexOf("查看详情") === -1 ? item.label.content : item.label.content.slice(0, item.label.content.length - 5);
        item.label.color = '#ffffff';
        item.label.fontSize = item.id !== 99999 ? 10 : 14;
        item.label.anchorX = -25;
        item.iconPath = item.id !== 99999 ? '../../../image/icon/xcAddress_x.png' : '../../../image/icon/dqAddress_x.png',
          item.width = item.id !== 99999 ? 14 : 30;
        item.height = item.id !== 99999 ? 14 : 30;
      });
      const obj = {
        id: 9999999,
        longitude: e.detail.longitude,
        latitude: e.detail.latitude,
        joinCluster: false, //是否参与聚合点
        iconPath: '../../../image/icon/xzAddress_x.png', //显示的图标
        width: 30, //图标的宽度
        height: 30, //图表的高度
        label: { //为标记点旁边增加标签
          content: ` `,
          color: '#ffffff',
          fontSize: 14,
          anchorX: 0,
          anchorY: 0,
        },
      };

      //判断是否已经添加过目的地
      const arrFlag = makerArr.some(item => item.id === 10000);

      //如果已经添加过目的地就修改目的地，没有就添加目的地
      if (arrFlag) {
        makerArr[makerArr.length - 1].longitude = e.detail.longitude;
        makerArr[makerArr.length - 1].latitude = e.detail.latitude;
      } else {
        makerArr.push(obj)
      };

      getlatlngAddress({
        longitude: e.detail.longitude,
        latitude: e.detail.latitude,
      }).then(res => {
        const {
          recommend,
          rough,
          standard_address
        } = res;
        this.setData({
          markerData: makerArr,
          longitude: e.detail.longitude,
          latitude: e.detail.latitude,
          "form.endPosition": recommend,
          "form.endLongitude": e.detail.longitude,
          "form.endLatitude": e.detail.latitude,
        });
        if (_this.boxIndex === 2) {
          this.setData({
            sectAddressData: {
              value: recommend,
              longitude: e.detail.longitude,
              latitude: e.detail.latitude,
            }
          });
          return;
        };
      });
    };
  },

  //重置地址图标点击事件
  czAddress() {
    this.setData({
      "form.endPosition": null,
      "form.endLongitude": null,
      "form.endLatitude": null,
    });
    this.getCurrentAddress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})